#ifndef UNIT_HPP_INCLUDED
#define UNIT_HPP_INCLUDED

#include <SFML/Graphics.hpp>
#include "MoveCase.hpp"
#include "Consts.h"
#include "Drawing.h"
#include "Player.h"
#include "Utility.h"
#include <string>
#include <cmath>

class Unit
{
private:
    sf::Vector2i m_pos;
    int m_life;
    int m_max_life;
    int m_attack;
    int m_current_attack_count ;
    int m_attack_count ;
    int m_current_move; // 0 : no movement left
    int m_movement_speed;
    int m_range;
    bool m_isHovered;
    bool m_isDead ;
    std::vector<MoveCase*> m_availableCase;
    bool m_isSelected;
    Player* m_owner;

    MoveCase* GetCaseFromPos(int x, int y);

    void testCase(int i, int pos[2], std::vector< std::vector<int> >* square, int ground[21][21]);

public:
    Unit();
    Unit(int attack, int life, int mov, int range, Player* owner);

    std::vector<MoveCase*>* getavailableCase() {return & m_availableCase;};
    int GetIdFromPos(int x, int y) const;
    int getLife() const {return m_life;};
    int getMaxLife() const {return m_max_life;};
    int getAttack() const {return m_attack;};
    int getMovementSpeed() const {return m_movement_speed;};
    int getCurrentMvmt() const {return m_current_move;};
    int getCurrentAttackCount( ) const {return m_current_attack_count ;};

    bool anyMoveAvalaible() const {return ( getCurrentMvmt() > 0 || getCurrentAttackCount() > 0 );};


    int getRange() const {return m_range;};
    bool getSelect() const {return m_isSelected;};
    bool getHovered() const {return m_isHovered;};
    sf::Vector2i getPos() const {return m_pos;};

    Player* getOwner() const {return m_owner;};

    bool isDead() const {return m_isDead;};
    sf::Vector2i getPosition() const {return m_pos;};

    //affiche les cases accessibles pour un deplacement

    bool isInRange(Unit* target, int range = -1);

    bool takeDamage(Unit * attacker);
    bool takeDamage(Unit * attacker , int dmg);
    void dealDamage(Unit * target);
    void dealDamage(Unit * target, int dmg);

    void resetMove();
    void setCurrentMvmt(int mvt) {m_current_move = mvt;};
    void setPosition(sf::Vector2i new_pos);
    void setPosition(int x, int y);
    void setSelect(bool s);
    void setHovered(bool s);
    void setDead(bool s);
    void setCurrentAttackCount( int ac ) {m_current_attack_count = ac ;};


    bool setOwner(Player* newowner) { if (newowner != NULL) {m_owner = newowner; return true;} else return false ; }

    void draw(Drawing* drawingmodule, sf::Color outlinecolor = sf::Color::Blue);
};

#endif // UNIT_HPP_INCLUDED
